<div class="container_12" id="body">
	<div class="grid_12">
	<form name="form_users" action="" method="post">
		<div style="text-align: right;">
			<button id="btn_toggle">Khóa/ Mở khóa</button> 
			<?php echo anchor('backend/user/create', 'Thêm mới', 'class="button"');?>
		</div>
		<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">
			<thead>
				<tr>
					<th width="1"><input type="checkbox" name="chk_all" /></th>
					<th width="150" class="header"><?php echo anchor('backend/user/load/username', 'Tài khoản');?></th>
					<th width="250" class="header"><?php echo anchor('backend/user/load/email', 'Email');?></th>
					<th width="150" class="header"><?php echo anchor('backend/user/load/role_name', 'Nhóm');?></th>
					<th class="header"><?php echo anchor('backend/user/load/created', 'Ngày đăng ký');?></th>
					<th width="75" class="header"><?php echo anchor('backend/user/load/banned', 'Trạng thái');?></th>
					<th width="60">&nbsp;</th>
				</tr>
			</thead>
			<tbody></tbody>
		</table>
		<div class="pagination"></div>
		</form>
	</div>
	<div class="clear"></div>
</div>
<script type="text/javascript">
var sort_dir = 'asc';
var ajax_url = null;
$(function() {
	$('.header').toggle(function() {
		$('.header').removeClass('headersortup headersortdown');
		$(this).toggleClass('headersortup');
		_load($(this).children().attr('href')+'/asc');
	}, function() {
		$('.header').removeClass('headersortup headersortdown');
		$(this).toggleClass('headersortdown');
		_load($(this).children().attr('href')+'/desc');
	});
	
	$('.pagination a').live('click', function() {
		_load($(this).attr('href'));
		return false;
	});

	$('input[name="chk_all"]').live('click', function() {
		$(this).parents('table').find('input:checkbox').attr('checked', $(this).is(':checked'));   
	});

	_load(base_url+current+'/load');

	$('#btn_toggle').live('click', function() {
		if(confirm('Khóa/ Mở khóa những tài khoản này?')) {
			_load(base_url+current+'/delete', $('form').serialize());
		}
		
		return false;
	});
});

function _load(url, params) {
	$.postJSON(url, params, function(data) {
		if(data != null && data.message == 'OK') {
			if(params == null) {
				ajax_url = url;
				$('input[name="chk_all"]').attr('checked', false);
				
				$('.tablesorter tbody').html(data.rows);
				$('.pagination').html(data.pagination);

				$('.tablesorter tbody tr').hover(
						function(){ $(this).children('td').addClass('row_hover'); }, 
						function(){ $(this).children('td').removeClass('row_hover'); }
				);
				$('.tablesorter tbody tr:odd > td').addClass('row_odd');
				$( ".edit" ).button({icons: {primary: "ui-icon-wrench"}, text: false});
			} else {
				_load(ajax_url);
			}
		} else {
			alert("Error");
		}
	});
}
</script>
